package com.xyhuo.leetcode.december;

import java.util.ArrayList;
import java.util.List;

/**
 * 子集
 * https://leetcode.cn/problems/subsets/
 *
 * @time 2023/12/28
 */
public class Subsets {

//    public static void main(String[] args) {
//        Subsets s = new Subsets();
//        int[] nums = {1, 2, 3, 4};
//        s.subsets(nums);
//    }

    public List<List<Integer>> subsets(int[] nums) {
        List<List<Integer>>[] dp = new List[nums.length + 1];
        for (int i = 0; i <= nums.length; i++) {
            List<List<Integer>> item = new ArrayList<>();
            dp[i] = item;
        }
        List<Integer> zeroItem = new ArrayList<>();
        dp[0].add(zeroItem);

        List<Integer> oneItem = new ArrayList<>();
        oneItem.add(nums[0]);
        dp[1].add(oneItem);

        for (int i = 1; i < nums.length; i++) {
            List<List<Integer>>[] tmpDp = new List[i + 2];
            for (int j = 0; j <= i; j++) {
                List<List<Integer>> tmp = new ArrayList<>();
                for (List<Integer> list : dp[j]) {
                    List<Integer> model = new ArrayList<>();
                    model.addAll(list);
                    model.add(nums[i]);
                    tmp.add(model);
                }
                tmpDp[j + 1] = tmp;
            }
            for (int j = 1; j <= i + 1; j++) {
                if (tmpDp[j] != null) {
                    dp[j].addAll(tmpDp[j]);
                }
            }
        }

        List<List<Integer>> ans=new ArrayList<>();
        for (List<List<Integer>> item : dp) {
            ans.addAll(item);
        }
        return ans;
    }
}
